//
// Created by DELL on 2021/3/12.
//

#ifndef SYLAR_FD_MANAGER_H
#define SYLAR_FD_MANAGER_H

#include <memory>
#include <vector>
#include "mutex.h"
#include "iomanager.h"
#include "singleton.h"
namespace sylar {
    class FdCtx : public std::enable_shared_from_this<FdCtx> {
    public:
        using ptr = std::shared_ptr<FdCtx>;
        explicit FdCtx(int fd);
        ~FdCtx();

        bool init();
        bool isInit()const{return m_isInit;}
        bool isSocket()const{return m_isSocket;}
        bool isClosed()const{return m_isClosed;}

        void setUserNonblock(bool v){m_useNonblock = v;}
        bool getUserNonblock() const{return m_useNonblock;}

        void setSysNonblock(bool v){m_sysNonblock = v;}
        bool getSysNonblock() const{return m_sysNonblock ;}

        void setTimeout(int type, uint64_t v);
        uint64_t getTimeout(int type);
    private:
        bool m_isInit : 1;
        bool m_isSocket : 1;
        bool m_sysNonblock : 1;
        bool m_useNonblock : 1;
        bool m_isClosed : 1;
        int m_fd;
        uint64_t m_recvTimeout;
        uint64_t m_sendTimeout;
        sylar::IOManager* m_manager;
    };
class FdManager{
public:
    using RWMutexType = RWMutex;
    FdManager();
    FdCtx::ptr get(int fd,bool auto_create = false);
    void del(int fd);

private:
    RWMutexType m_mutex;
    std::vector<FdCtx::ptr> m_data;

};
using FdMgr = Singleton<FdManager>;
}
#endif //SYLAR_FD_MANAGER_H
